package com.lc.cehui.ui.activity.land;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.LoginApi;
import com.lc.cehui.http.response.LoginBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.MainActivity;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.segcontrolbutton.SegmentedControlItem;
import com.lc.cehui.widget.segcontrolbutton.SegmentedControlView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

public class LoginActivity extends AppActivity {


    @BindView(R.id.scv)
    SegmentedControlView scv;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    private int msg = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        List<SegmentedControlItem> days = createDays();
        scv.addItems(days);
        scv.setOnSegItemClickListener(new SegmentedControlView.OnSegItemClickListener() {
            @Override
            public void onItemClick(SegmentedControlItem item, int position) {
                switch (position) {
                    case 0:
                        msg = 1;
                        break;
                    case 1:
                        msg = 2;
                        break;
                }


            }
        });
    }

    private List<SegmentedControlItem> createDays() {
        List<SegmentedControlItem> items = new ArrayList<>();
        items.add(new SegmentedControlItem("企业"));
        items.add(new SegmentedControlItem("个人"));
        return items;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_submit, R.id.tv_register, R.id.tv_forget})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_submit:
//                if (CourseUtils.isNullOrEmpty(etPhone.getText().toString())){
//                    toast("手机号不能为空");
//                    return;
//                }
//                if (CourseUtils.isNullOrEmpty(etPassword.getText().toString())){
//                    toast("密码不能为空");
//                    return;
//                }
                if (msg == 1) {
                    SPUtil.put(LoginActivity.this, IntentKey.RID, 1);
                } else {
                    SPUtil.put(LoginActivity.this, IntentKey.RID, 2);
                }
                EasyHttp.post(this)
                        .api(new LoginApi().setMobile(etPhone.getText().toString()).setPassword(etPassword.getText().toString()))
                        .request(new HttpCallback<LoginBean>(this) {
                            @Override
                            public void onSucceed(LoginBean data) {
                                LogUtils.e(JSON.toJSONString(data));
                                if (callBackCode(data)) {
                                    SPUtil.put(LoginActivity.this, IntentKey.USERID, data.getData().getUserid());
                                    SPUtil.put(LoginActivity.this, IntentKey.TOKEN, data.getData().getToken());
                                    SPUtil.put(LoginActivity.this, IntentKey.TYPE, data.getData().getType());
                                    String token;
                                    if (msg == 1) {
                                        token = "yqIuyscDKXb3rldlU7f3+OuiBpr+v7tk@as51.cn.rongnav.com;as51.cn.rongcfg.com";
                                    } else {
                                        token = "XjcdpD0R85j3rldlU7f3+JNhvOEgTQY7@as51.cn.rongnav.com;as51.cn.rongcfg.com";
                                    }
                                    RongIM.connect(token, new RongIMClient.ConnectCallback() {
                                        @Override
                                        public void onSuccess(String userId) {
                                            // 登录成功，跳转到默认会话列表页。
                                            LogUtils.e("登陆成功" + userId);
                                        }

                                        @Override
                                        public void onError(RongIMClient.ConnectionErrorCode connectionErrorCode) {
                                            LogUtils.e("登陆失败" + connectionErrorCode);
                                        }

                                        @Override
                                        public void onDatabaseOpened(RongIMClient.DatabaseOpenStatus databaseOpenStatus) {
                                            LogUtils.e(databaseOpenStatus);
                                        }
                                    });

                                    startActivity(MainActivity.class);
                                }
                            }
                        });

                break;
            case R.id.tv_register:
                startActivity(RegisterWelcomeActivity.class);
                break;
            case R.id.tv_forget:
                startActivity(ForgetPasswordActivity.class);
                break;
        }
    }
}
